package com.wicket.okrcomponent.biz.service.nb;
 import com.wicket.okrcomponent.biz.service.dto.common.CurrentCycleDto;
 import com.wicket.okrcomponent.biz.service.dto.common.SumOfMultipleNumDto;
 import com.wicket.okrcomponent.common.util.CommonFunctionHelper;
 import org.springframework.stereotype.Service;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import com.wicket.okrcomponent.biz.service.dto.req.CalculateTimeMigrationReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.CalculateTimeMigrationRespDto;
import com.wicket.okrcomponent.biz.service.dto.req.ObtainCurrentCycleReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ObtainCurrentCycleRespDto;

 import java.math.BigDecimal;
 import java.util.*;
import com.wicket.okrcomponent.biz.service.dto.req.AddSumOfMultipleNumServiceReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.AddSumOfMultipleNumServiceRespDto;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbCalculationRules {
/**
      * code:timeMigration
      * name:M2-计算时间偏移得最终时间
      * desc:undefined
      * gen by moon at 8/28/2022, 4:03:03 PM
      * 
      **/
     @Trace(operationName = "M2-计算时间偏移得最终时间")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public CalculateTimeMigrationRespDto calculateTimeMigration(CalculateTimeMigrationReqDto reqDto) {
         CalculateTimeMigrationRespDto retData = new CalculateTimeMigrationRespDto();
         Date result = null;
         if(reqDto.getTimeDiffDays()!=null){
             Long sec = Long.valueOf(reqDto.getTimeDiffDays())*24*3600;
             result = CommonFunctionHelper.addDateBySec(reqDto.getCaluStartTime(),sec.intValue()-1);
         }else if(reqDto.getTimeDiffHours()!=null){
             Long sec = Long.valueOf(reqDto.getTimeDiffDays())*3600;
             result = CommonFunctionHelper.addDateBySec(reqDto.getCaluStartTime(),sec.intValue()-1);
         }
         retData.setCalculateMigrationTime(result);
         return retData;
      }
/**
      * code:getTargetCycle
      * name:M2获取当前周期
      * desc:undefined
      * gen by moon at 8/28/2022, 4:03:14 PM
      * 
      **/
     @Trace(operationName = "M2获取当前周期")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ObtainCurrentCycleRespDto obtainCurrentCycle(ObtainCurrentCycleReqDto reqDto) {
         ObtainCurrentCycleRespDto retData = new ObtainCurrentCycleRespDto();
         List<CurrentCycleDto> sortedList = new ArrayList<>(reqDto.getCurrentCycleList());
         List<CurrentCycleDto> filteredList = new ArrayList<>();
         for(CurrentCycleDto one : sortedList){
             if(one.getIsLastCycle().equals("TRUE")){
                 filteredList.add(one);
             }
         }
         CurrentCycleDto lastCycle = null;
         for (int i = filteredList.size() - 1; i >= 0; i--) {
             CurrentCycleDto one = filteredList.get(i);
             if(reqDto.getSystemCurrentTime().compareTo(one.getCycleStartTime())>=0 && reqDto.getSystemCurrentTime().compareTo(one.getCycleEndTime())<=0){
                 retData.setCurrentCycleId(one.getCycleId());
                 if(lastCycle!=null){
                     retData.setNextCycleId(lastCycle.getCycleId());
                 }
                 break;
             }
             lastCycle = one;
         }

         lastCycle = null;
         for (int i = sortedList.size() - 1; i >= 0; i--) {
             CurrentCycleDto one = sortedList.get(i);
             if(!one.getCycleTypeCode().equals(reqDto.getSubcycleType())){
                 continue;
             }
             if(reqDto.getSystemCurrentTime().compareTo(one.getCycleStartTime())>=0 && reqDto.getSystemCurrentTime().compareTo(one.getCycleEndTime())<=0){
                 retData.setCurrentSubCycleId(one.getCycleId());
                 if(lastCycle!=null){
                     retData.setNextSubCycleId(lastCycle.getCycleId());
                 }
                 break;
             }
             lastCycle = one;
         }
         return retData;

      }
/**
      * code:sumOfMultipleNum
      * name:M2-新增数据集求和服务（特殊方法）
      * desc:undefined
      * gen by moon at 12/5/2023, 12:05:42 AM
      *
      **/
     @Trace(operationName = "M2-新增数据集求和服务（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public AddSumOfMultipleNumServiceRespDto addSumOfMultipleNumService(AddSumOfMultipleNumServiceReqDto reqDto) {
         AddSumOfMultipleNumServiceRespDto retData = new AddSumOfMultipleNumServiceRespDto();
         int mode = -1;
         if(reqDto.getDecimalDigitsLastRules()!=null){
             mode = BigDecimal.ROUND_HALF_UP;
             if(reqDto.getDecimalDigitsLastRules().equals("MALING")){
                 mode = BigDecimal.ROUND_DOWN;
             }
         }

         BigDecimal calcResult = new BigDecimal(0);
         for (SumOfMultipleNumDto one : reqDto.getSumOfMultipleNumList()) {
             calcResult = calcResult.add(new BigDecimal(one.getCalcPara1()));
         }
         if(mode!=-1){
             calcResult = calcResult.setScale(reqDto.getDecimalPointRules().intValue(),mode);
         }
         retData.setCalcResult(calcResult.doubleValue());
         return retData;
      }
    //手工接入方法
}
